<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>画板</title>
    <style>
        * {
            padding: 0;
            margin: 0;
            box-sizing: border-box;
        }

        #canvas {
            /* 和屏幕一样高 */
            /* border: 1px solid red; */
        }
    </style>
</head>

<body>
    <canvas id="canvas" width="100px" height="100px"></canvas>
    <script>
        let canvas = document.getElementById("canvas");
        canvas.height = document.documentElement.clientHeight;
        canvas.width = document.documentElement.clientWidth;
        console.log(canvas.width);
        let ctx = canvas.getContext("2d");
        ctx.strokeStyle = 'none';
        ctx.fillStyle = "black";
        ctx.lineCap = "round";
        ctx.lineWidth = 4;
        let painting = false;
        let last;
        var isTouchDevice = 'ontouchstart' in document.documentElement;
        function drawLine(x1, y1, x2, y2) {
            ctx.beginPath();
            ctx.moveTo(x1, y1);
            ctx.lineTo(x2, y2);
            ctx.stroke();
        }
        if (isTouchDevice) {
            canvas. ontouchstart = (e) => {
                let x = e.touches[0].clientX;
                let y = e.touches[0].clientY;
                last = [x, y];
            }
            canvas.ontouchmove = (e) => {
                let x = e.touches[0].clientX;
                let y = e.touches[0].clientY;
                // ctx.beginPath();
                // ctx.arc(x, y, 10, 0, 2 * (Math.PI));
                // ctx.stroke();
                // ctx.fill();
                // let x1 = e.touches[1].clientX;
                // let y1 = e.touches[1].clientY;
                // ctx.beginPath();
                // ctx.arc(x1, y1, 10, 0, 2 * (Math.PI));
                // ctx.stroke();
                // ctx.fill();
                drawLine(last[0], last[1], x, y);
                last = [x, y];
            }
        } else {
            canvas.onmousedown = (e) => {
                painting = true;
                last = [e.clientX, e.clientY];
            }
            canvas.onmouseup = () => {
                painting = false;
            }
            canvas.onmousemove = (e) => {
                if (painting === true) {
                    // ctx.beginPath();
                    // ctx.arc(e.clientX, e.clientY, 10, 0, 2 * (Math.PI));
                    // ctx.stroke();
                    // ctx.fill();
                    drawLine(last[0], last[1], e.clientX, e.clientY);
                    last = [e.clientX, e.clientY];

                } else {

                }
            }
        }
    </script>
</body>

</html>